package com.seeyon.kk.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;

public class UploadUtils {

	public static String upload(MultipartFile myFile) {
		String myFilePath = "";
		String filename ="";
		if (!myFile.isEmpty()) {
			try {
				SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
				filename = myFile.getOriginalFilename();
				filename =df.format(new Date())+"-"+ UUID.randomUUID() + filename.substring(filename.lastIndexOf("."));
				// 本地路径
				// myFilePath = "F:/V7.0SP3/V7.0SP3/ApacheJetspeed/webapps/seeyon/apps_res/plugin/tinymce/images/image/" + filename;
				// 服务器路径
				myFilePath="/ucapp/seeyon/A8/ApacheJetspeed/webapps/seeyon/apps_res/plugin/tinymce/images/image/"+ filename;
				BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(myFilePath)));
				out.write(myFile.getBytes());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "https://uc.locknlock.com.cn/seeyon/apps_res/plugin/tinymce/images/image/" + filename;
	}
	
	public static String uploadCase(MultipartFile myFile) {
		String myFilePath = "";
		String filename ="";
		if (!myFile.isEmpty()) {
			try {
				SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
				filename = myFile.getOriginalFilename();
				filename =df.format(new Date())+"-"+ UUID.randomUUID() + filename.substring(filename.lastIndexOf("."));
				String path="/OAFiles/A8/Approval/"+new SimpleDateFormat("yyyy-MM-dd/").format(new Date());
				//如果不存在,创建文件夹
				File f = new File(path);
				if(!f.exists()){
				    f.mkdirs(); 
				}
				// 本地路径
				// myFilePath = "F:/V7.0SP3/V7.0SP3/ApacheJetspeed/webapps/seeyon/apps_res/plugin/tinymce/images/image/" + filename;
				// 服务器路径
				myFilePath=path + filename;
				BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(myFilePath)));
				out.write(myFile.getBytes());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "http://uc.locknlock.com.cn:8082/A8/Approval/"+new SimpleDateFormat("yyyy-MM-dd/").format(new Date()) + filename;
	}
}
